package cn.tedu.ivos2.audit.service.impl;

import cn.tedu.ivos2.application.pojo.entity.Application;
import cn.tedu.ivos2.audit.mapper.AuditMapper;
import cn.tedu.ivos2.audit.pojo.entity.Audit;
import cn.tedu.ivos2.audit.pojo.vo.AuditVO;
import cn.tedu.ivos2.audit.service.AuditService;
import cn.tedu.ivos2.base.enums.AuditStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AuditServiceImpl implements AuditService {
    @Autowired
    private AuditMapper  auditMapper;
    @Override
    public List<AuditVO> queryAuditByApplicationId(Long id) {
        return auditMapper.queryAuditByApplicationId(id);
    }

    @Override
    public void addAudit(Application application) {
        List<Long> userIds = application.getAuditUserIdList();
        for (int i = 0; i<userIds.size();i++) {
            Audit audit = new Audit();
            audit.setApplicationId(application.getId());
            audit.setAuditUserId(userIds.get(i));
            audit.setAuditSort(i);
            if (i == 0) {
                audit.setAuditStatus(AuditStatusEnum.MY_PENDING.getCode());
            }  else {
                audit.setAuditStatus(AuditStatusEnum.PENDING.getCode());
            }
            audit.setCreateTime(new Date());
            auditMapper.insertAudit(audit);
        }
    }

    public void deleteAuditByApplicationId(Long id) {
        auditMapper.deleteAuditByApplicationId(id);
    }
}
